<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>加入购物车</title>
</head>
<body>
<input id="goodsid" placeholder="商品编号"><br/>
<input id="goodsname" placeholder="商品名称"><br/>
<input id="goodsnum" placeholder="商品数量"><br/>
<input id="goodsprice" placeholder="商品价格"><br/>
<input id="goodsimg" placeholder="商品图片"><br/>
<button id="btn">新增</button>
</body>
<script src="js/cookie.js"></script>
<script>
    var goodsidInput = document.getElementById("goodsid");
    var goodsnameInput = document.getElementById("goodsname");
    var goodsnumInput = document.getElementById("goodsnum");
    var goodspriceInput = document.getElementById("goodsprice");
    var goodsimgInput = document.getElementById("goodsimg");
    var btn = document.getElementById("btn");
    btn.onclick = function () {
        //略过表单验证
        var goodsid = goodsidInput.value;
        var goodsname = goodsnameInput.value;
        var goodsnum = goodsnumInput.value;
        var goodsprice = goodspriceInput.value;
        var goodsimg = goodsimgInput.value;

        //新增的时候要注意  名称不能重复 要保证唯一性
        if (getCookie("goodsid_" + goodsid)) { //有的话就做更新
            var existNum = getCookie("goodsnum_" + goodsid);
            var updateNum = (existNum*1) + (goodsnum*1);
            setCookie("goodsnum_"+goodsid,updateNum,7);
            window.open("myshoppingcar.html");

        } else { //没有的就做新增
            //完全形态的新增
            setCookie("goodsid_" + goodsid, goodsid, 7);
            setCookie("goodsname_" + goodsid, goodsname, 7);
            setCookie("goodsnum_" + goodsid, goodsnum, 7);
            setCookie("goodsprice_" + goodsid, goodsprice, 7);
            setCookie("goodsimg_" + goodsid, goodsimg, 7);
            window.open("myshoppingcar.html");
        }
    }


</script>
</html>